package com.iwop.moto.controller;

import com.alibaba.fastjson.JSON;
import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.dao.MPromotionDao;
import com.iwop.moto.model.MPromotion;
import com.iwop.moto.model.PO.PromotionVo;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.MPromotionService;
import com.iwop.moto.service.UserService;
import com.iwop.moto.utils.stringUtil.StringUtils;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author czq
 * @version 1.0
 * @created 2017/2/14 0014.
 */
@Controller
public class MpromotionController extends BaseController {
    org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(MpromotionController.class);

    @Autowired
    MPromotionDao mPromotionDao;
    @Autowired
    UserService userService;
    @Autowired
    MPromotionService mPromotionService;


    /**
     * 获取上级用户信息
     *
     * @param userId
     * @return
     */
    @RequestMapping("/api/getSuperUserByUserId")
    @ResponseBody
    private MPromotion getSuperUserByUserId(@ApiParam(name = "user_id", value = "用户id")
                                            @RequestParam(value = "user_id") String user_id) {
        MPromotion mPromotion = mPromotionDao.getSuperUserByFromUser(user_id);

        return mPromotion;
    }

    /**
     * 获取分销详情
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/api/promotionDetil", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel getPromotionInfo(@ApiParam(name = "userId", value = "用户id")
                                        @RequestParam(value = "userId") String userId) {
        if (StringUtils.isEmpty(userId)) {
            return null;
        }
        PromotionVo userPromotionByUserId = userService.findUserPromotionByUserId(userId);
        if (userPromotionByUserId == null) {
            return getErr("没有分销收益相关数据....");
        }
        return getSucceed(userPromotionByUserId, ErrCode.SUCCEED);
    }

    /**
     * 获取下级用户数详情列表
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/api/promotionUserDetil", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel getPromotionUserDetil(@ApiParam(name = "userId", value = "用户id")
                                             @RequestParam(value = "userId") String userId,
                                             @ApiParam(name = "rows", value = "分页大小")
                                             @RequestParam(value = "rows") int rows,
                                             @ApiParam(name = "pages", value = "当前页")
                                             @RequestParam(value = "pages") int pages) {
        if (StringUtils.isEmpty(userId)) return null;
        if (rows <= 0) rows = 10;
        if (pages <= 0) pages = 1;
        int offset = (pages - 1) * rows;
        SuperRecordModel srModel = new SuperRecordModel();
        srModel.put("offset", offset);
        srModel.put("maxrow", rows);
        srModel.put("userId", userId);
        List<SuperRecordModel> childUserInfoList = mPromotionService.findChildUserInfoByUserId(srModel);
        if (childUserInfoList == null) {
            return getSucceed(childUserInfoList, "没有相关数据");
        }
        Long count = mPromotionService.countPayMent(srModel);
        count = ((count - 1) / rows) + 1;
        ResultModel resultModel = new ResultModel();
        resultModel.setData(JSON.toJSONString(childUserInfoList));
        resultModel.setCode(1);
        resultModel.setAnnex(count + "");
        return resultModel;
    }

    @RequestMapping(value = "/api/cash", method = RequestMethod.POST)
    @ResponseBody
    public ResultModel cash(@ApiParam(name = "userId", value = "用户id")
                            @RequestParam(value = "userId") String userId
    ) {
        if (StringUtils.isEmpty(userId)) {
            return getErr("参数错误。。。");
        }
        Boolean aBoolean = userService.cashUserProceedsMoney(userId);
        if (aBoolean) {
            return getSucceed(ErrCode.SUCCEED);
        }
        return getErr("系统错误");
    }
}
